package com.dxd.servlet.swzl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dxd.constant.ErrorCodeConstant;
import com.dxd.entity.ReturnBean;
import com.dxd.entity.UserInfo;
import com.dxd.servlet.PublicServlet;

/**
 * 前端用户管理
 * 
 * @author pppcar
 *
 */
@WebServlet("/swzl/userInfo")
public class UserInfoServlet extends PublicServlet {

	private static final long serialVersionUID = -2754603844053914575L;
	private static final Logger LOG = LoggerFactory.getLogger(UserInfoServlet.class);

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String type = request.getParameter("type");
		if ("goLogin".equals(type)) {

			// 跳转到登录页面
			Dispatcher("/view/swzl/login.jsp");
			return;
		} else if ("login".equals(type)) {

			// 登录方法
			String userName = request.getParameter("userName");// 登录用户名
			String passWord = request.getParameter("passWord");// 密码

			if (userName == null || "".equals(userName)) {
				request.setAttribute("loginMsg", "用户名为空");
				Dispatcher("/view/swzl/login.jsp");
				return;
			}

			if (passWord == null || "".equals(passWord)) {
				request.setAttribute("loginMsg", "密码为空");
				Dispatcher("/view/swzl/login.jsp");
				return;
			}

			UserInfo userInfo = userInfoDao.Login(userName, passWord, 0);// 此时的0表示非管理员用户

			if (userInfo != null && userInfo.getUserName() != null) {

				if (userInfo.getState() == 0) {
					request.setAttribute("loginMsg", "该账号已经被停用");
					Dispatcher("/view/swzl/login.jsp");
					return;
				}

				// 验证成功
				HttpSession session = request.getSession();
				session.setAttribute("swzlUser", userInfo);
				session.setAttribute("swzlUserId", userInfo.getId());
				// 设置登录小时失效
				session.setMaxInactiveInterval(60 * 60 * 3);
				Redirect("/LostandFoundManage/swzl/index");
				return;

			} else {

				request.setAttribute("loginMsg", "用户名或密码错误");
				Dispatcher("/view/swzl/login.jsp");
				return;
			}

		} else if ("loginOut".equals(type)) {

			LOG.info("退出系统，清空session");
			getSession().setAttribute("swzlUser", null);
			getSession().setAttribute("swzlUserId", null);
			Dispatcher("/view/swzl/login.jsp");
			return;
		} else if ("goRegister".equals(type)) {

			LOG.info("跳转注册页面，type：{},url:{}", type, request.getContextPath());
			Dispatcher("/view/swzl/register.jsp");
			return;

		} else if ("register".equals(type)) {

			// 用户注册
			String userName = request.getParameter("user_name");
			String passWord = request.getParameter("pass_word");
			String name = request.getParameter("name");
			String sex = request.getParameter("sex");
			String phone = request.getParameter("phone");
			String address = request.getParameter("address");
			String province = request.getParameter("province");
			String city = request.getParameter("city");
			String area = request.getParameter("area");
			String age = request.getParameter("age");
			String headImg = request.getParameter("headImg");

			if (headImg == null || "".equals(headImg)) {
				request.setAttribute("registerMsg", "请上传头像");
				Dispatcher("/view/swzl/register.jsp");
				return;
			}
			if (userName == null || "".equals(userName)) {
				request.setAttribute("registerMsg", "请填写用户名");
				Dispatcher("/view/swzl/register.jsp");
				return;
			}
			if (passWord == null || "".equals(passWord)) {
				request.setAttribute("registerMsg", "请填写密码");
				Dispatcher("/view/swzl/register.jsp");
				return;
			}
			if (name == null || "".equals(name)) {
				request.setAttribute("registerMsg", "请填写真实姓名");
				Dispatcher("/view/swzl/register.jsp");
				return;
			}
			if (phone == null || "".equals(phone)) {
				request.setAttribute("registerMsg", "请填电话");
				Dispatcher("/view/swzl/register.jsp");
				return;
			}
			if (province == null || "".equals(province) || city == null || "".equals(city) || area == null
					|| "".equals(area) || address == null || "".equals(address)) {
				request.setAttribute("registerMsg", "请填写地址");
				Dispatcher("/view/swzl/register.jsp");
				return;
			}
			// 验证用户名是否存在
			UserInfo userInfo = userInfoDao.queryByUserName(userName);
			if (userInfo != null && userInfo.getId() != null) {
				request.setAttribute("registerMsg", "用户名已经存在");
				Dispatcher("/view/swzl/register.jsp");
				return;
			}
			userInfo = new UserInfo();
			userInfo.setUserName(userName);
			userInfo.setPassWord(passWord);
			userInfo.setAddress(address);
			userInfo.setAge(age == null ? 0 : Integer.parseInt(age));
			userInfo.setArea(area);
			userInfo.setCity(city);
			String[] headImgs = headImg.split("\\/");
			userInfo.setHeadImg(headImgs[headImgs.length - 1]);
			userInfo.setIsAdmin(0);
			userInfo.setName(name);
			userInfo.setPhone(phone);
			userInfo.setProvince(province);
			userInfo.setSex(sex);
			userInfo.setState(1);
			userInfoDao.addUser(userInfo);
			LOG.info("用户注册成功，用户名：{}", userName);
			Dispatcher("/view/swzl/login.jsp");
			return;

		} else if ("myInfo".equals(type)) {
			// 查询我的详细信息
			UserInfo userInfo = getUserInfoSwzl();
			if (userInfo == null || userInfo.getId() == null) {
				Redirect(request.getContextPath() + "/swzl/userInfo?type=goLogin");
				return;
			}
			request.setAttribute("myInfo", userInfo);
			Dispatcher("/view/swzl/userInfo.jsp");
			return;

		} else if ("updateUser".equals(type)) {
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String passWord = request.getParameter("pass_word");//
			String name = request.getParameter("name");//
			String sex = request.getParameter("sex");//
			String phone = request.getParameter("phone");//
			String address = request.getParameter("address");
			String province = request.getParameter("province");
			String city = request.getParameter("city");
			String area = request.getParameter("area");
			String age = request.getParameter("age");
			String id = request.getParameter("id");
			
			if(id==null){
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("id为空");
				returnJson(returnBean);
				return;
			}
			UserInfo userInfo= userInfoDao.findById(Integer.parseInt(id));
			if(userInfo==null||userInfo.getId()==null){
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("当前用户不存在");
				returnJson(returnBean);
				return;
			}
			String headImg = request.getParameter("headImg");
			if (headImg == null || "".equals(headImg)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请上传头像");
				returnJson(returnBean);
				return;
			}
			if (passWord == null || "".equals(passWord)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("密码不能为空");
				returnJson(returnBean);
				return;
			}
			if (name == null || "".equals(name)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("姓名不能为空");
				returnJson(returnBean);
				return;
			}
			if (phone == null || "".equals(phone)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("电话不能为空");
				returnJson(returnBean);
				return;
			}
			if (province == null || "".equals(province) || city == null || "".equals(city) || area == null
					|| "".equals(area) || address == null || "".equals(address)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("地址不能为空");
				returnJson(returnBean);
				return;
			}
			// 验证用户名是否存在
			userInfo.setPassWord(passWord);
			userInfo.setAddress(address);
			userInfo.setAge(age == null ? 0 : Integer.parseInt(age));
			userInfo.setArea(area);
			userInfo.setCity(city);
			String[] headImgs = headImg.split("\\/");
			userInfo.setHeadImg(headImgs[headImgs.length - 1]);
			userInfo.setName(name);
			userInfo.setPhone(phone);
			userInfo.setProvince(province);
			userInfo.setSex(sex);
			userInfo.setId(id == null ? 0 : Integer.parseInt(id));
			userInfoDao.updateUser(userInfo);
			LOG.info("修改用户成功，用户名：{}", id);
			returnJson(returnBean);
			return;
		}
	}

}